/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.knoppers.examples;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;

/**
 *
 */
public class FileUploadForm extends org.apache.struts.action.ActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4123631138411732285L;

	private FormFile file;
	private String message;

	/**
     * 
     */
	public FileUploadForm() {
		super();
	}

	/**
	 * @return the file
	 */
	public FormFile getFile() {
		return file;
	}

	/**
	 * @param file
	 *            the file to set
	 */
	public void setFile(FormFile file) {
		this.file = file;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message
	 *            the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.struts.action.ActionForm#validate(org.apache.struts.action
	 * .ActionMapping, javax.servlet.http.HttpServletRequest)
	 */
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		final ActionErrors errors = new ActionErrors();
		if (file.getFileSize() == 0) {
			errors.add("file", new ActionMessage("error.file.required"));
		} else if (!file.getContentType().equals("application/vnd.ms-excel")) {
			errors.add("file", new ActionMessage("error.file.type"));
		} else if (file.getFileSize() > 20480) {
			errors.add("file", new ActionMessage("error.file.size"));
		}
		return errors;
	}
}
